<template>
  <div>
    <el-tree :data="menus" :props="defaultProps" :expand-on-click-node="false" :show-checkbox="true" node-key="catId"
             draggable :allow-drop="allowDrop" @node-drop="handleDrop">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button type="text" size="mini" @click="() => appendMenus(data)">添加</el-button>
          <el-button type="text" size="mini" @click="() => edit(data)">修改</el-button>
          <el-button type="text" size="mini" @click="() => removeMenus(node, data)" v-if="node.level>1">删除</el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
      <el-form :model="category">
        <el-form-item label="分类名称">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标地址">
          <el-input v-model="category.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input v-model="category.productUnit" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitData()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      updateNodes: [],
      maxLevel: 0,
      dialogType: '',
      dialogVisible: false,
      title: '',
      category: {
        catId: null,
        name: '',
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        icon: '',
        productUnit: ''
      },
      menus: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  methods: {
    getMenus () {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get'
      }).then(({data}) => {
        this.menus = data.data
      })
    },
    submitData () {
      if (this.dialogType === 'add') {
        this.addCategory()
      } else if (this.dialogType === 'edit') {
        this.updateCategory()
      }
    },
    appendMenus (data) {
      this.dialogVisible = true
      this.category = {}
      this.category.parentCid = data.catId
      this.category.catLevel = data.catLevel + 1
      this.category.showStatus = 1
      this.dialogType = 'add'
      this.title = '添加分类'
    },
    addCategory () {
      this.$http({
        url: this.$http.adornUrl('/product/category/save'),
        data: this.category,
        method: 'post'
      }).then(({data}) => {
        this.$message({
          type: data.status,
          message: data.msg
        })
        this.getMenus()
        this.dialogVisible = false
      })
    },
    edit (data) {
      this.dialogVisible = true
      this.dialogType = 'edit'
      this.title = '修改分类'
      this.category.name = data.name
      this.category.catId = data.catId
      this.category.parentCid = data.parentCid
      this.category.catLevel = data.catLevel
      this.category.showStatus = data.showStatus
      this.category.sort = data.sort
      this.category.icon = data.icon
      this.category.productUnit = data.productUnit
    },
    updateCategory () {
      let {catId, name, icon, productUnit} = this.category
      this.$http({
        url: this.$http.adornUrl('/product/category/update'),
        method: 'post',
        data: this.$http.adornData({catId, name, icon, productUnit})
      }).then(({data}) => {
        this.$message({
          type: data.status,
          message: data.msg
        })
        this.getMenus()
        this.dialogVisible = false
        this.category.icon = data.icon
        this.category.productUnit = data.productUnit
        this.expandeKey = [this.category.parentCid]
      })
    },
    removeMenus (node, data) {
      this.dialogType = 'remove'
      this.$confirm(`是否【${data.name}】删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = [data.catId]
        this.$http({
          url: this.$http.adornUrl('/product/category/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          this.$message({
            type: data.status,
            message: data.msg
          })
          this.getMenus()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    allowDrop (draggingNode, dropNode, type) {
      this.countNodeLevel(draggingNode.data)
      let deep = this.maxLevel - draggingNode.data.catLevel + 1
      if (type === 'inner') {
        return (deep + dropNode.level) <= 3
      } else {
        return (deep + dropNode.parent.level) <= 3
      }
    },
    countNodeLevel (draggingNode) {
      let childrenNodes = draggingNode.children
      if (childrenNodes != null && childrenNodes.length > 0) {
        childrenNodes.forEach(item => {
          if (item.catLevel > this.maxLevel) {
            this.maxLevel = item.catLevel
          }
          this.countNodeLevel(item)
        })
      }
    },
    handleDrop (draggingNode, dropNode, dropType, ev) {
      this.updateNodes = []
      let pCid = 0
      let siblings = []
      if (dropType === 'inner') {
        pCid = dropNode.data.catId
        siblings = dropNode.childNodes
      } else {
        siblings = dropNode.parent.childNodes
        pCid = dropNode.parent.data.catId === undefined ? 0 : dropNode.parent.data.catId
      }
      for (let i = 0; i < siblings.length; i++) {
        let node = {catId: '', parentCid: '', sort: ''}
        if (siblings[i].data.catId === draggingNode.data.catId) {
          let catLevel = draggingNode.level
          if (siblings[i].level !== draggingNode.level) {
            catLevel = siblings[i].level
            this.updateChildNodeLevel(siblings[i])
          }
          node = {catId: siblings[i].data.catId, sort: i, parentCid: pCid, level: catLevel}
        } else {
          node = {catId: siblings[i].data.catId, sort: i}
        }
        this.updateNodes.push(node)
      }
      this.$http({
        url: this.$http.adornUrl('/product/category/update/sort'),
        data: this.updateNodes,
        method: 'post'
      }).then(({data}) => {
        this.$message({
          type: 'success',
          message: '菜单顺序修改成功'
        })
        this.getMenus()
        this.expandeKey = [pCid]
      })
    },
    updateChildNodeLevel (node) {
      if (node.childNodes.length > 0) {
        node.childNodes.forEach(item => {
          this.updateNodes.push({catId: item.data.catId, catLevel: item.level})
          this.updateChildNodeLevel(item)
        })
      }
    }
  },
  created () {
    this.getMenus()
  }
}
</script>

<style scoped>

</style>
